package com.sl.dev.modular.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.dev.modular.file.entity.DevFileBucket;
import com.sl.dev.modular.file.mapper.DevFileBucketMapper;
import com.sl.dev.modular.file.service.DevFileBucketService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;

@Service
public class DevFileBucketServiceImpl extends ServiceImpl<DevFileBucketMapper, DevFileBucket> implements DevFileBucketService {

    @SneakyThrows
    @Override
    public String storageFileWithReturnUrl(String bucketName, String fileId, InputStream file) {
        byte[] bytes = IoUtil.readBytes(file);
        DevFileBucket devFileBucket = new DevFileBucket();
        devFileBucket.setFileId(fileId);
        devFileBucket.setFileByte(bytes);
        super.save(devFileBucket);
        return fileId;
    }

    @Override
    public DevFileBucket queryByFileId(String fileId) {
        LambdaQueryWrapper<DevFileBucket> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DevFileBucket::getFileId, fileId);
        return super.getOne(lambdaQueryWrapper);
    }
}
